<?php
/**
 * https://gitee.com/litephp
 * http://www.dazhetu.cn/
 * jay_fun 410136330@qq.com
 * Date: 2019/1/11
 * Time: 15:33
 */

namespace app\admin\controller\content;

use app\common\controller\BaseAdmin;
use app\common\model\content\Withdrawal as WithdrawalModel;
use LiteAdmin\Music163;
use think\db;

/**
 * @title 提现管理
 * Class Music
 * @package app\admin\controller\content
 */
class Withdrawal extends BaseAdmin
{
    /**
     * @title 列表页
     * @return mixed
     */
    public function index()
    {
        $db = WithdrawalModel::alias('r')
            ->join('content_user u','u.id = r.uid','left')
            ->join('content_payment p','p.id = r.sid','left')
            ->field('r.*,u.uname,p.payment,p.opening_bank,p.opening_name,p.card_num,p.pay_img,p.pay_num')
            ->order('create_time desc');

        $search = $this->request->get();
        // 精准查询
        foreach (['status'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->where('r.'.$field,'=', $search[$field]);
            }
        }
        // 模糊查询
        foreach (['uname'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->whereLike('u.'.$field, "%{$search[$field]}%");
            }
        }
        return $this->_list($db, true, $search);
    }




    /**
     * @title 审核充值记录
     */
    public function change()
    {
        $id = $this->request->post('id');
        $state = $this->request->post('status');

        $this->tongguo($id,$state);
    }
    public function tongguo($id,$state){

        if($state == 1){
            db()->startTrans();
            $list = db('content_withdrawal')->where(array('id'=>$id))->find();
            $b = db('content_balance')->where(array('uid'=>$list['uid']))->find();
            $balance = db('content_balance')->where(array('uid'=>$list['uid']))->setDec('mineral',$list['price']);
            $arr = [
                'uid'=>$list['uid'],
                'content'=>'提现-'.$list['price'],
                'front'=>$b['mineral'],
                'after'=>$b['mineral']-$list['price'],
                'type'=>2,
                'create_time'=>time(),
                'sign' => 2,
                'number'=>$list['price']
            ];
            $log = db('content_balance_log')->insert($arr);
            $change =  $this->_change(new WithdrawalModel(), $id, ['status' => $state]);
            if($balance && $change && $log){
                db()->commit();
            }else{
                db()->rollback();
                $this->error('切换状态操作失败！');
            }

        }else{
            $this->_change(new WithdrawalModel(), $id, ['status' => $state]);
        }



    }
    /**
     * @title 删除提现记录
     */
    public function del()
    {
        $ids = $this->request->post('ids',false);
        !$ids && $this->error("缺少参数！");

        $this->_del(new WithdrawalModel(),$ids);
    }
}